<?php
declare (strict_types = 1);

namespace app\api\model\v1;

use think\Model;

/**
 * @mixin \think\Model
 */
class TopicExerciseLog extends AppModel
{

    //题型：1判断题，2单选题，3多选题，4填空题，5问答题
    const TOPIC_ONE = 1;
    const TOPIC_TWO = 2;
    const TOPIC_THREE = 3;
    const TOPIC_FOUR = 4;
    const TOPIC_FIVE = 5;


    public static $is_topic = array(
        self::TOPIC_ONE => '判断题',
        self::TOPIC_TWO => '单选题',
        self::TOPIC_THREE => '多选题',
        self::TOPIC_FOUR => '填空题',
        self::TOPIC_FIVE => '问答题',

    );


    //
    public function option()
    {
        return $this->hasMany(LearnOption::class,'learn_topic_id','learn_topic_id');
    }

    public function user(){
        return $this->belongsTo(Users::class,'exercise_user_id')->field('id,nickname,unit_id');
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/26 9:41
     * @param $option
     * @param $answer
     * @param $user_answer
     * @return array
     * 转换选项
     */
    public static function changeOption($option, $answer, $user_answer)
    {
        $option_key = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $option_arr = json_decode($option, true);

        $option_ids_arr = [];
        foreach ($option_arr as $key => $value) {
            $option_ids_arr[$value['id']] = $option_key[$key];
        }

        $answer_arr = explode(',', $answer); //正确答案

        $new_answer = "";
        foreach ($answer_arr as $k => $v) {
            $new_answer .= $option_ids_arr[$v] . ',';
        }
        $new_my_answer = "";
        if ($user_answer !== '') {
            $my_answer = explode(',', $user_answer);

            foreach ($my_answer as $k1 => $v1) {
                $new_my_answer .= $option_ids_arr[$v1] . ',';
            }
            $new_my_answer = rtrim($new_my_answer, ',');
        }

        $item = [
            'new_answer' => rtrim($new_answer, ','),
            'new_my_answer' => $new_my_answer,
        ];
        return $item;
    }
}
